<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //Promise的基本使用方式
        function ajax(url) {
            //创建一个Promise对象
            var promiseObj = new Promise((resolve, reject) => {
                //resolve代表的是成功回调
                //reject 代表的是失败回调
                //创建一个ajax对象
                var xhr = new XMLHttpRequest();
                //连接服务器
                xhr.open("get", url, true);
                //发送数据
                xhr.send(null)
                //接受返回值
                xhr.onreadystatechange = function () {
                    if (xhr.readyState != 4) return;
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        //处理成功的请求
                        resolve(xhr.responseText)
                    } else {
                        //说明请求失败
                        reject("请求错误")
                    }
                }

            })
            return promiseObj
        }

        ajax("http://localhost:3000/data").then((data) => {
            console.log(data);
            return ajax("http://localhost:3000/data1")
        }).then((data1) => {
            console.log(data1);
            return ajax("http://localhost:3000/data2")
        }).then((data2) => {
            console.log(data2);
        })

    </script>
</body>

</html>